package com.market.sys.api.client;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.market.sys.common.Result;
import com.market.sys.entity.Comment;
import com.market.sys.entity.SubComment;
import com.market.sys.entity.User;
import com.market.sys.service.ICommentService;
import com.market.sys.service.ISubCommentService;
import com.market.sys.service.IUserService;
import com.market.sys.util.Constants;
import com.market.sys.util.EmptyUtil;
import com.market.sys.util.TimeUtil;
import com.market.sys.vo.CommentVO;
import com.market.sys.vo.SubCommentVO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.market.sys.util.Constants.DOWNLOAD_URL;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xd
 * @since 2024-03-26
 */
@RestController
@RequestMapping("/sys/comment")
public class CommentController {

    @Resource
    private ICommentService commentService;

    @PostMapping("/createComment")
    public Result<?> createComment(@RequestBody Map<String,Object> paramMap){
        Comment comment = BeanUtil.fillBeanWithMap(paramMap, new Comment(), false);
        comment.setTime(TimeUtil.timeYmdhms(new Date()));
        comment.setState(0);
        if (commentService.save(comment)){
            return Result.success("发表成功，正在审核中");
        }else {
            return Result.error("系统繁忙，发表失败");
        }
    }

    @PostMapping("/getCommentListByFatherId")
    public Result<?> fetchCommentListByFatherId(@RequestBody Map<String,Object> paramMap){
        Comment comment = BeanUtil.fillBeanWithMap(paramMap, new Comment(), false);
        return Result.success(commentService.fetchCommentList(comment.getThingId(),comment.getState()));
    }

    @PostMapping("/getCommentDetail")
    public Result<?> fetchCommentDetail(@RequestBody Map<String,Object> paramMap){
        Integer id = Integer.parseInt((String) paramMap.get("id"));
        CommentVO commentVO = commentService.fetchCommentDetail(id);
        return Result.success(commentVO);
    }

    @PostMapping("/deleteComment")
    public Result<?> deleteComment(@RequestBody Map<String,Object> paramMap){
        if (commentService.remove(new LambdaQueryWrapper<Comment>().eq(Comment::getId, String.valueOf(paramMap.get("id"))))) {
            return Result.success("删除成功");
        }else {
            return Result.error("系统繁忙，删除失败");
        }
    }
}
